<script>
import {changeEmployee, getEmployeeList} from "@/api/system.js";

export default {
  name: "EmployeeManage",
  data() {
    return {
      employeeData: [],
      form: {
        first_name: ''
      }
    }
  },
  created() {
    this.getEmployeeData()
  },
  methods: {
    getEmployeeData() {
      if(this.form.first_name === null) {
        getEmployeeList().then(res => {
          this.employeeData = res
        }).catch(err => {
          console.log(err)
        })
      } else {
        getEmployeeList(this.form).then(res => {
          this.employeeData = res
        }).catch(err => {
          console.log(err)
        })
      }
    },
    changeEmployeeStatus(row) {
      if(row.is_active) {
        row.is_active = false
        changeEmployee(row).then(() => {
          this.getEmployeeData()
        }).catch(err => {
          console.log(err)
        })
      } else {
        row.is_active = true
        changeEmployee(row).then(() => {
          this.getEmployeeData()
        }).catch(err => {
          console.log(err)
        })
      }
    }
  }
}
</script>

<template>
  <div class="app-container">
    <el-form ref="form" :model="form" @keyup.enter="getEmployeeData">
      <el-row :gutter="15">
        <el-col :span="6">
          <el-form-item>
            <el-input placeholder="请输入员工姓名" style="width: 200px" v-model="form.first_name" clearable />
            <el-button icon="Search" @click="getEmployeeData" />
          </el-form-item>
        </el-col>
        <el-col :span="6" :offset="12">
          <div class="button-group">
            <el-form-item>
              <el-button type="primary" icon="Plus">新增员工</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="danger" icon="Delete">删除员工</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Upload">导入</el-button>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <el-table :data="employeeData" style="width: 100%;margin-top: 20px">
      <el-table-column fixed label="ID" prop="id" width="80px" align="center" />
      <el-table-column label="姓名" prop="first_name" align="center" />
      <el-table-column label="手机号" prop="last_name" align="center" />
      <el-table-column label="邮箱" prop="email" align="center" />
      <el-table-column label="职位" prop="position" align="center" />
      <el-table-column label="状态" prop="is_active" align="center">
        <template v-slot="scope">
          <el-tag :type="scope.row.is_active ? 'success' : 'danger'">{{ scope.row.is_active ? '在职' : '离职' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="240px" align="center">
        <template v-slot="scope">
          <el-button type="primary" text icon="Edit">编辑</el-button>
          <el-button type="warning" text icon="Switch" @click="changeEmployeeStatus(scope.row)">
            {{ scope.row.is_active ? '改为离职' : '改为在职' }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<style scoped>
.button-group {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>
